import {defineConfig, loadEnv} from 'vite'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import {ElementPlusResolver} from 'unplugin-vue-components/resolvers'

export default defineConfig(({mode, command}) => {
    const {VITE_PORT, VITE_SERVER} = loadEnv(mode, process.cwd())
    return {
        plugins: [vue(), AutoImport({
            imports: ["vue", "vue-router",  "pinia"],
            resolvers: [ElementPlusResolver()],
        }),
            Components({
                resolvers: [ElementPlusResolver()],
            })],
        resolve: {
            alias: {
                '@': '/src',
                '@view': '/src/views',
                '@api': '/src/api',
                '@store': '/src/store',
                '@router': '/src/router',
                '@assets': '/src/assets',
            },
        },
        server: {
            port: VITE_PORT,
            hmr: true,
            host: '0.0.0.0',
            open: true,
            proxy: {
                '^/api': {
                    target: VITE_SERVER,
                    secure: false,
                    changeOrigin: true,
                    rewrite: (path) => path.replace(/^\/api/, ''),
                },
            },
        }
    }
})
